package com.demo284_窥视迭代器;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class PeekingIterator implements Iterator<Integer> {

    private List<Integer> elementDataList = new ArrayList<>();

    private int size;

    private int current;

    public PeekingIterator(Iterator<Integer> iterator) {
        while (iterator.hasNext()) {
            elementDataList.add(iterator.next());
        }
        size = elementDataList.size();
    }

    public Integer peek() {
        return elementDataList.get(current+1);
    }

    @Override
    public Integer next() {
        Integer peek = peek();
        current++;
        return peek;
    }

    @Override
    public boolean hasNext() {
        return current<size;
    }
}